package com.myapp.ruyixing.ui;

import static android.view.View.GONE;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Model;
import com.myapp.ruyixing.model.UserInfo;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.LoginHelper;

public class UserActivity extends Activity implements OnClickListener {
	private String mUserId;
	private String mUserName;

	private RYHttpResponseHandler mFollowHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Model model = (Model) o;
				onDoFollowFinished(true, model);
			} catch (Exception e) {
				onDoFollowFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onDoFollowFinished(false, null);
		}
	};

	private void doFollow(String bduss, String aid, int status) {
		showLoading(true, "加载中");
		RYHttpClient.doFollow(this, mFollowHttpResponseHandler, bduss, aid,
				status);
	}

	private void onDoFollowFinished(boolean succeed, Model model) {
		showLoading(false, "加载中");
		if (succeed && model != null && model.errno == 0) {
			String text = ((Button) findViewById(R.id.btn_follow)).getText()
					.toString();
			((Button) findViewById(R.id.btn_follow)).setText("取消关注"
					.equals(text) ? "关注" : "取消关注");
		} else {
			showToast("操作失败 " + (model == null ? "" : model.msg));
		}
	}

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				UserInfo info = (UserInfo) o;
				onLoadingDataFinished(true, info);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void onLoadingDataStarted(String userId, String bduss) {
		showLoading(true, "加载中");
		RYHttpClient.getOthersUserInfo(this, mHttpResponseHandler, userId,
				bduss);
	}

	private void onLoadingDataFinished(boolean succeed, UserInfo userInfo) {
		showLoading(false, "加载中");
		if (succeed && userInfo != null) {
			loadView(userInfo);
		} else {
			showToast("加载失败！");
		}
	}

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	private void showLoading(boolean show, String msg) {
		if (isFinishing()) {
			return;
		}
		if (show) {
			findViewById(R.id.layout_loading).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.text_loading)).setText(msg);
		} else {
			findViewById(R.id.layout_loading).setVisibility(GONE);
		}
	}

	private void loadView(UserInfo userInfo) {

		findViewById(R.id.text_shoucang).setOnClickListener(this);
		findViewById(R.id.text_guanzhu).setOnClickListener(this);
		findViewById(R.id.text_fensi).setOnClickListener(this);
		findViewById(R.id.layout_qiandao).setOnClickListener(this);
		findViewById(R.id.layout_dianping).setOnClickListener(this);
		findViewById(R.id.layout_zhaopian).setOnClickListener(this);
		findViewById(R.id.btn_follow).setOnClickListener(this);
		findViewById(R.id.btn_fasixin).setOnClickListener(this);

		if (!TextUtils.isEmpty(userInfo.username)) {
			((TextView) (findViewById(R.id.text_name)))
					.setText(userInfo.username);
			mUserName = userInfo.username;
		}
		if ("男".equals(userInfo.sex)) {
			findViewById(R.id.text_name).setSelected(true);
		} else {
			findViewById(R.id.text_name).setSelected(false);
		}

		if (userInfo.status == 2) {
			((Button) findViewById(R.id.btn_follow)).setText("取消关注");
		} else {
			((Button) findViewById(R.id.btn_follow)).setText("关注");
		}

		if (!TextUtils.isEmpty(userInfo.avatar)) {
			RequestQueue mQueue = Volley.newRequestQueue(this);
			ImageLoader mImageLoader = new ImageLoader(mQueue,
					new BitmapLruCache());
			((NetworkImageView) findViewById(R.id.img_avatar)).setImageUrl(
					userInfo.avatar, mImageLoader);
		}

		if (!TextUtils.isEmpty(userInfo.livecity)) {
			findViewById(R.id.text_city).setVisibility(View.VISIBLE);
			((TextView) (findViewById(R.id.text_city))).setText("常居地："
					+ userInfo.livecity);
		} else {
			findViewById(R.id.text_city).setVisibility(View.GONE);
		}

		((TextView) (findViewById(R.id.text_photo))).setText(userInfo.photo
				+ "");

		((TextView) (findViewById(R.id.text_review))).setText(userInfo.review
				+ "");

		((TextView) (findViewById(R.id.text_signin))).setText(userInfo.signin
				+ "");

	}

	public UserActivity() {
		// Required empty public constructor
	}

	public static void startUserActivity(Context context, String userId) {
		if (context == null || TextUtils.isEmpty(userId)) {
			return;
		}
		Intent intent = new Intent(context, UserActivity.class);
		intent.putExtra("userid", userId);
		context.startActivity(intent);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);

		mUserId = getIntent().getStringExtra("userid");

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});

		onLoadingDataStarted(mUserId, LoginHelper.getInstance().getBDUSS());
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {

		case R.id.btn_follow:
			String text = ((Button) findViewById(R.id.btn_follow)).getText()
					.toString();
			doFollow(LoginHelper.getInstance().getBDUSS(), mUserId,
					"取消关注".equals(text) ? 2 : 1);
			break;

		case R.id.btn_fasixin:
			intent = new Intent(this, DirectMessageDetailActivity.class);
			intent.putExtra("name", mUserName);
			startActivity(intent);
			break;

		case R.id.text_shoucang:
			intent = new Intent(this, FavoriteActivity.class);
			intent.putExtra("userid", mUserId);
			startActivity(intent);
			break;
		case R.id.text_guanzhu:
			intent = new Intent(this, FollowActivity.class);
			intent.putExtra("userid", mUserId);
			startActivity(intent);
			break;
		case R.id.text_fensi:
			intent = new Intent(this, FansActivity.class);
			intent.putExtra("userid", mUserId);
			startActivity(intent);
			break;
		case R.id.layout_qiandao:
			intent = new Intent(this, RegistrationActivity.class);
			intent.putExtra("userid", mUserId);
			startActivity(intent);
			break;
		case R.id.layout_dianping:
			intent = new Intent(this, RemarkActivity.class);
			intent.putExtra("userid", mUserId);
			startActivity(intent);
			break;
		case R.id.layout_zhaopian:
			intent = new Intent(this, PhotoActivity.class);
			intent.putExtra("userid", mUserId);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
}
